import Vue from 'vue';
import VueRouter from 'vue-router';
import ElementUI from 'element-ui';

Vue.use(VueRouter);
export const indexChild = [
  {
    path: "/menu",
    component: () => import('../views/menu/index.vue'),
    meta: {
      title: "菜单管理"
    }
  }, {
    path: "/role",
    component: () => import('../views/role/index.vue'),
    meta: {
      title: "角色管理"
    }

  }, {
    path: "/admin",
    component: () => import('../views/user/index.vue'),
    meta: {
      title: "管理员管理"
    }
  }, {
    path: "/cate",
    component: () => import('../views/cate/index.vue'),
    meta: {
      title: "商品分类"
    }
  }, {
    path: "/specs",
    component: () => import('../views/specs/index.vue'),
    meta: {
      title: "商品规格"
    }
  }, {
    path: "/goods",
    component: () => import('../views/goods/index.vue'),
    meta: {
      title: "商品管理"
    }
  }, {
    path: "/member",
    component: () => import('../views/member/index.vue'),
    meta: {
      title: "会员管理"
    }
  }, {
    path: "/banner",
    component: () => import('../views/banner/index.vue'),
    meta: {
      title: "轮播图管理"
    }
  }, {
    path: "/seckill",
    component: () => import('../views/seckill/index.vue'),
    meta: {
      title: "秒杀活动"
    }
  },
];

const routes = [
  {
    path: "/login",
    component: () => import('../pages/login.vue'),
    meta: {
      title: "登录"
    }
  }, {
    path: "/index",
    component: () => import('../pages/index.vue'),
    children: [
      {
        path: "/home",
        component: () => import('../views/home.vue'),
        meta: {
          title: "首页"
        }
      },
      ...indexChild
      , {
        path: '',
        redirect: '/home'
      }
    ]
  }, {
    path: '*',
    redirect: '/home'
  }
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

//全局登录拦截
router.beforeEach((to, from, next) => {
  document.title = to.meta.title;
  //判断是否是去往登录 
  if (to.path == "/login") {
    next();
    return
  }
  if (sessionStorage.getItem('userInfo')) {
    //局部拦截
    let url = JSON.parse(sessionStorage.getItem('userInfo')).menus_url.some(val => {
      return val == to.path
    });
    if (!url && to.path != '/home') {
      ElementUI.Message.error("您没有当前权限，已为您自动跳转")
      next('/home');
      return
    }
    next()
    return
  }

  next('/login');



});


export default router;
